<?php

namespace frontendm\controllers;

/**
 * @author  shuang
 * @date    2016-11-3 11:19:44
 * @version V1.0
 * @desc    
 */
use common\components\WXController;
use Yii;
use frontendm\models\WB_UserProfile;
use common\models\User;
use dosamigos\qrcode\QrCode;
use yii\helpers\Json;

class ProfileController extends WXController {

    public function actionInfo() {
        $model = WB_UserProfile::find()->where("userid=" . Yii::$app->user->id)->one();
        return $this->render("info", ["model" => $model]);
    }

    public function actionUpdatepwd() {
        if (Yii::$app->request->isAjax) {
            $model = new \frontendm\models\ResetPasswordForm(Yii::$app->user->id);
            $model->oldpassword = Yii::$app->request->post("oldpassword");
            $model->password = Yii::$app->request->post("password");
            $model->repassword = Yii::$app->request->post("repassword");
            if ($model->validate()) {
                if ($model->resetPassword()) {
                    echo Json::encode(["status" => true, "msg" => "密码修改成功！", "url" => Yii::$app->getHomeUrl()]);
                } else {
                    echo Json::encode(["status" => false, "msg" => "密码修改失败！"]);
                }
            } else {
                foreach ($model->errors as $item) {
                    $message = $item[0];
                    break;
                }
                echo Json::encode(["status" => false, "msg" => $message]);
            }
            exit;
        }
        return $this->render("updatepwd");
    }

    public function actionXianxia() {
        $res = WB_UserProfile::getOfflineList();
        return $this->render("xianxia", $res);
    }

    public function actionInvite() {
        $count = WB_UserProfile::find()->where("invite_code=:invite_code",[":invite_code"=>Yii::$app->user->identity->my_code])->count();
        return $this->render("invite",["mytuicount"=>$count ? $count : 0]);
    }

    public function actionQrcode() {
        return QrCode::png('http://www.toupiao518.cn/site/signup.html?id=' . Yii::$app->user->identity->my_code);    //调用二维码生成方法
    }

}
